package com.tencent.web.controller;

import com.github.pagehelper.PageInfo;
import com.tencent.config.IdUtil;
import com.tencent.entity.CheckGroup;
import com.tencent.entity.Checkitem;
import com.tencent.entity.Pagination;
import com.tencent.entity.Result;
import com.tencent.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Fame
 * @date: 2022-08-11 15:57
 * @description:
 */
@RestController
@RequestMapping("/check")
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    @RequestMapping("/findAll")
    public Result findAll(Pagination pagination){
        PageInfo<CheckGroup> all = checkGroupService.getAll(pagination);
//        Result result = new Result(true, "ok", all);
        return Result.success(all);
    }

    @RequestMapping("/addOne")
    public int addBean(CheckGroup checkGroup){
//        int i = checkGroupService.insertChecks(checkGroup);
//        return checkGroupService.insert(checkGroup);

        int insert = checkGroupService.insert(checkGroup);

        IdUtil.tl.set(checkGroup.getId());

        return insert;
    }

    @PostMapping("/delOne")
    public int delBean(String id){
        return checkGroupService.delHandle(id);
    }

    @GetMapping("/findById")
    public CheckGroup getBeanById(Integer id){
        return checkGroupService.findById(id);
    }

    @PostMapping("/updateById")
    public int updateBean(CheckGroup checkGroup){
        return  checkGroupService.updateBean(checkGroup);
    }
    @GetMapping("findChecks")
    public Result getChecks(Integer id){
        List<Checkitem> checks = checkGroupService.findChecks(id);
        return Result.success(checks);
    }

    @GetMapping("/findAddChecks")
    public Result findAddChecks(){
        List<Checkitem> addChecks = checkGroupService.findAddChecks();
        return Result.success(addChecks);
    }

    @PostMapping("/addGI")
    public int insertGI( @RequestParam(value = "ids[]", required = false) Integer... val){

        if(null != val){
            Integer id = IdUtil.tl.get();
            if (id != null){
                for (int i = 0; i < val.length; i++) {

                    checkGroupService.insertChecks(id,val[i]);
                }
            }
            return val.length;
        }
        return 0;
    }
}
